package com.ruige.account.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruige.account.entity.NameValue;
import com.ruige.account.entity.QueryVo;
import com.ruige.account.entity.Result;
import com.ruige.account.entity.StatusCode;
import com.ruige.account.pojo.AccountLog;
import com.ruige.account.service.IAccountLogService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author huangrui
 * @since 2020-05-25
 */
@RestController
@RequestMapping("/accountLog")
public class AccountLogController {


    @Autowired
    private IAccountLogService accountLogService;


    @RequestMapping("/findAll")
    public Result findAll() {
        List<AccountLog> accountLogs = accountLogService.findAll();
        return new Result(true, StatusCode.OK, "查询成功!", accountLogs);
    }

    /**
     * 基础查询,查询当前登录人的余额,年月日消费信息等
     *
     * @return
     */
    @RequestMapping("/baseInfo")
    public Result baseInfo(@RequestBody QueryVo QueryVo) {
        Map<String, BigDecimal> baseInfo = accountLogService.baseInfo(QueryVo.getUserId());
        return new Result(true, StatusCode.OK, "查询成功!", baseInfo);
    }

    /**
     * 查询账单
     *
     * @return
     */
    @RequestMapping("/findLogByPage")
    public Result findLogByPage(@RequestParam(value = "page", required = false) Long page,
                                @RequestParam(value = "rows", required = false) Long rows,
                                @RequestBody QueryVo QueryVo) {

        IPage<AccountLog> result = accountLogService.findLogByPage(page, rows, QueryVo.getUserId());
        return new Result(true, StatusCode.OK, "查询成功!", result);
    }

    /**
     * 删除账单
     *
     * @return
     */
    @PostMapping("/deleteById")
    public Result deleteById(@RequestParam(value = "id") Integer id) {
        Integer delete = accountLogService.deleteById(id);
        if (delete > 0) {
            return new Result(true, StatusCode.OK, "删除成功!");
        } else {
            return new Result(false, StatusCode.ERROR, "删除失败!");
        }
    }


    /**
     * 查询最近一周消费图表数据
     *
     * @return
     */
    @RequestMapping("/getWeekChartData")
    public Result getWeekChartData(@RequestBody QueryVo queryVo) {

        Map<String, List> result = accountLogService.getWeekChartData(queryVo.getUserId());
        return new Result(true, StatusCode.OK, "查询成功!", result);

    }


    /**
     * 查询某月累积消费图表数据
     *
     * @return
     */
    @RequestMapping("/getCurveChartData")
    public Result getCurveChartData(@RequestBody QueryVo queryVo) {
        if (queryVo.getDate() == null ||
                (queryVo.getDate().getMonthValue() == LocalDate.now().getMonthValue() && queryVo.getDate().getYear() == LocalDate.now().getYear())) {
            queryVo.setDate(LocalDate.now());
        } else {
            queryVo.setDate(queryVo.getDate().with(TemporalAdjusters.lastDayOfMonth()));
        }

        Map<String, List> result = accountLogService.getCurveChartData(queryVo.getUserId(), queryVo.getDate());
        return new Result(true, StatusCode.OK, "查询成功!", result);
    }


    /**
     * 查询占比图表数据
     *
     * @return
     */
    @RequestMapping("/getRatioChartData")
    public Result getRatioChartData(@RequestBody QueryVo QueryVo) {

        Result result = null;
        if (QueryVo.getDate() == null && QueryVo.getYear() == null) {
            QueryVo.setDate(LocalDate.now());
            List<NameValue> resultData = accountLogService.getRatioChartData(QueryVo.getUserId(), QueryVo.getDate());
            result = new Result(true, StatusCode.OK, "查询成功!", resultData);
            return result;
        }
        if (QueryVo.getDate() != null && QueryVo.getYear() == null) {
            List<NameValue> resultData = accountLogService.getRatioChartData(QueryVo.getUserId(), QueryVo.getDate());
            result = new Result(true, StatusCode.OK, "查询成功!", resultData);
            return result;
        }
        if (QueryVo.getDate() == null && QueryVo.getYear() != null) {
            LocalDate year = LocalDate.of(QueryVo.getYear(), 1, 1);
            List<NameValue> resultYearData = accountLogService.getRatioYearChartData(QueryVo.getUserId(), year);
            result = new Result(true, StatusCode.OK, "查询成功!", resultYearData);
            return result;
        }
        return result;


    }

}
